package com.vinjogames.ui;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.PointF;

import com.vinjogames.entities.Game;
import com.vinjogames.gfx.Camera;
import com.vinjogames.gfx.Font;
import com.vinjogames.gfx.Image;
import com.vinjogames.gfx.SpriteSheet;
import com.vinjogames.util.GameConVars;

public class BuyMenu{
	
	float mGLWidth;
	float mGLHeight;
	PointF mBotLeft;
	PointF mTopRight;
//	PointF budgetLocation;
//	PointF resourcesRemainingLocation;
	Image mBackground;
	Game mGame;
	int index = 0;
	
	PressableButton[][][] gridButtons;
//	PressableButton techButton;
//	PressableButton closeButton;
	PressableButton pageLeft;
	PressableButton pageRight;
	
	int selectedTank;
	
	public BuyMenu(Game game){
		mGame = game;
		mGLWidth = Camera.GL_WIDTH;
		mGLHeight = Camera.GL_HEIGHT;

		mBotLeft = new PointF(0,0);
		mTopRight = new PointF(mGLWidth,mGLHeight);
		
		selectedTank = 0;
	}
	
	//TODO: Change up the inner for loop to switch between the types of stuff displayed in the buy menu
	//(namely units for the first 9 entries and then base upgrades for the last.)
	public void load(){
		SpriteSheet ss = SpriteSheet.getInstance();

//		budgetLocation = new PointF(8,4.75f);
//		resourcesRemainingLocation = new PointF(8,4.25f);
		mBackground = new Image(GameConVars.MENUBACKGROUND,mGLWidth, mGLHeight);

		gridButtons = new PressableButton[GameConVars.BUY_PAGES][GameConVars.BUY_HEIGHT][GameConVars.BUY_WIDTH];
		for(int page = 0; page < GameConVars.BUY_PAGES; ++page)
			for(int row = 0; row < GameConVars.BUY_HEIGHT; ++row)
				for(int column = 0; column < GameConVars.BUY_WIDTH; ++column) {
					if(column + row * GameConVars.BUY_WIDTH >= GameConVars.GRAPHIC_TANKS.length) return;
					gridButtons[page][row][column] = new PressableButton(new Image(
							GameConVars.GRAPHIC_TANKS[column + row * GameConVars.BUY_WIDTH],
									GameConVars.BUY_BUTTON_WIDTH, GameConVars.BUY_BUTTON_HEIGHT),
									GameConVars.GRAPHIC_TANKS_OFFSET + (column + row * GameConVars.BUY_WIDTH),
									new PointF(1 + column * GameConVars.BUY_BUTTON_WIDTH, 1 + row * GameConVars.BUY_BUTTON_HEIGHT),
									new PointF(1 + (column + 1) * GameConVars.BUY_BUTTON_WIDTH, 1 + (row + 1) * GameConVars.BUY_BUTTON_HEIGHT), true);
				}
//		techButton = new PressableButton(new Image(GameConVars.GRAPHIC_WINDOW_BORDER, mTopRight.x, 1), -1, new PointF(0,mTopRight.y - 1), mTopRight, false);
//		techButton.setText("Tech Menu");
//		closeButton = new PressableButton("X", new PointF(mTopRight.x - 1, 0), new PointF(mTopRight.x, 1));
	}
	
	public void draw(GL10 gl){
		//gl.glColor4f(1, 1, 1, .7f);
		mBackground.draw(gl, new PointF(mBotLeft.x + (mTopRight.x - mBotLeft.x)/2, 
				                        mBotLeft.y + (mTopRight.y - mBotLeft.y)/2));
		
//		Font.drawString(gl, "Budget: " + (mGame.getCurrentPlayer().getMyTechnologyTree().getTechBudget()), budgetLocation, 0.33f);
//		Font.drawString(gl, "Resources: " + (mGame.getCurrentPlayer().getResourcesRemaining()), resourcesRemainingLocation, 0.33f);
		
		for(int p = 0; p < GameConVars.BUY_PAGES; ++p)
			for(int r = 0; r < GameConVars.BUY_HEIGHT; ++r)
				for(int c = 0; c < GameConVars.BUY_WIDTH; ++c)
					if(gridButtons[p][r][c] != null)
						gridButtons[p][r][c].draw(gl);
		
//		techButton.draw(gl);
//		closeButton.draw(gl);
		//gl.glColor4f(1, 1, 1, 1f);
	}
	
	public void drawTest(GL10 gl){
		mBackground.draw(gl, new PointF(mBotLeft.x + (mTopRight.x - mBotLeft.x)/2, 
                mBotLeft.y + (mTopRight.y - mBotLeft.y)/2));

		for(int p = 0; p < GameConVars.BUY_PAGES; ++p)
			for(int r = 0; r < GameConVars.BUY_HEIGHT; ++r)
				for(int c = 0; c < GameConVars.BUY_WIDTH; ++c)
					if(gridButtons[p][r][c] != null)
						gridButtons[p][r][c].draw(gl);
	}
		
	public void handleAction(int action, PointF glLocation) throws Exception{
		if(action == GameConVars.INPUT_TAP){
			PressableButton tappedButton = null;
			for(int p = 0; p < GameConVars.BUY_PAGES; ++p)
				for(int r = 0; r < GameConVars.BUY_HEIGHT; ++r)
					for(int c = 0; c < GameConVars.BUY_WIDTH; ++c)
						if(gridButtons[p][r][c] != null && gridButtons[p][r][c].insideBox(glLocation)) tappedButton = gridButtons[p][r][c];
//			if(techButton.insideBox(glLocation)) tappedButton = techButton;
//			if(closeButton.insideBox(glLocation)) tappedButton = closeButton;
			if(tappedButton == null) return;
			int num = tappedButton.getmImageNum();
			if(num >= GameConVars.GRAPHIC_TANKS_OFFSET && num < GameConVars.GRAPHIC_TANKS_OFFSET + GameConVars.GRAPHIC_TANKS.length) {
				parseTankButtonActions(num - GameConVars.GRAPHIC_TANKS_OFFSET);
			}
			else if(num >= GameConVars.GRAPHIC_BASEUP_OFFSET && num < GameConVars.GRAPHIC_BASEUP_OFFSET + GameConVars.GRAPHIC_BASEUPS.length) {
				// call a helper function to parse base upgrades using num - tanks offset.
			}
			else if (num == -1){
				mGame.getGUI().toggleMenu(index);				
			}
//			else if(tappedButton == closeButton){
//				mGame.getGUI().toggleMenu(-1);
//			}
			
//			if(one.insideBox(glLocation)){
//				mGame.addUnit(mGame.getCurrentPlayer(), Unit.TANK);
//			}
//			if(two.insideBox(glLocation)){
//				mGame.addUnit(mGame.getCurrentPlayer(), Unit.TANK2);
//			}
//			if(three.insideBox(glLocation)){
//				mGame.addUnit(mGame.getCurrentPlayer(), Unit.TANK3);
//			}
		}
	}
	
	protected void parseTankButtonActions(int tankNum) throws Exception {
		switch(tankNum) {
		case GameConVars.UNIT_SMALL_TANK:
			if(mGame.getCurrentPlayer().purchaseUnit(GameConVars.BUY_SmallTankCost))
				mGame.addUnit(GameConVars.UNIT_SMALL_TANK);
			break;
		case GameConVars.UNIT_MEDIUM_TANK:
			if(mGame.getCurrentPlayer().purchaseUnit(GameConVars.BUY_MediumTankCost))
				mGame.addUnit(GameConVars.UNIT_MEDIUM_TANK);
			break;
		case GameConVars.UNIT_HARVESTER_TANK:
			if(mGame.getCurrentPlayer().purchaseUnit(GameConVars.BUY_HarvesterTankCost))
				mGame.addUnit(GameConVars.UNIT_HARVESTER_TANK);
			break;
		case GameConVars.UNIT_SCOUT_TANK:
			if(mGame.getCurrentPlayer().purchaseUnit(GameConVars.BUY_ScoutTankCost))
				mGame.addUnit(GameConVars.UNIT_SCOUT_TANK);
			break;
		case GameConVars.UNIT_SABO_TANK:
			if(mGame.getCurrentPlayer().purchaseUnit(GameConVars.BUY_SaboteurgeTankCost))
				mGame.addUnit(GameConVars.UNIT_SABO_TANK);
			break;
		case GameConVars.UNIT_ARTILLARY:
			if(mGame.getCurrentPlayer().purchaseUnit(GameConVars.BUY_ArtillaryTankCost))
				mGame.addUnit(GameConVars.UNIT_ARTILLARY);
			break;
		case GameConVars.UNIT_MARKET_TANK:
			if(mGame.getCurrentPlayer().purchaseUnit(GameConVars.BUY_MarketTankCost))
				mGame.addUnit(GameConVars.UNIT_MARKET_TANK);
			break;
		case GameConVars.UNIT_FALCON_CHOPPER:
			if(mGame.getCurrentPlayer().purchaseUnit(GameConVars.BUY_FalconChopperCost))
				mGame.addUnit(GameConVars.UNIT_FALCON_CHOPPER);
			break;
		case GameConVars.UNIT_BEHEMOTH_TANK:
			if(mGame.getCurrentPlayer().purchaseUnit(GameConVars.BUY_BehemothTankCost))
				mGame.addUnit(GameConVars.UNIT_BEHEMOTH_TANK);
			break;
		}
	}

	public boolean insideBox(PointF p){
		return p.x >= mBotLeft.x && p.y >= mBotLeft.y & p.x <= mTopRight.x && p.y <= mTopRight.y;
	}
	
}
